package team.seekers.hula.course.resource.sectionContent.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import team.seekers.hula.common.annotation.ApiResponseBody;
import team.seekers.hula.course.course.dao.CourseDao;
import team.seekers.hula.course.course.entity.dao.CourseDO;
import team.seekers.hula.course.resource.sectionContent.controller.vo.SectionBindContextDTO;
import team.seekers.hula.course.resource.sectionContent.entity.dto.SectionContentDTO;
import team.seekers.hula.course.resource.sectionContent.entity.dto.SectionContentUpdateDTO;
import team.seekers.hula.course.resource.sectionContent.entity.vo.SectionContentVO;
import team.seekers.hula.course.resource.sectionContent.service.SectionContentManageService;
import team.seekers.hula.course.structure.section.dao.SectionDao;
import team.seekers.hula.course.structure.section.entity.dto.SectionDTO;

import java.util.List;

/**
 * @author GH
 * date: 2022/5/11 15:30
 */
@Controller
@ApiResponseBody
@RequestMapping("/manage/section/content")
public class SectionContentManageController {

    @Autowired
    private SectionContentManageService sectionContentManageService;

    @Autowired
    private SectionDao sectionDao;

    @Autowired
    private CourseDao courseDao;

    /**
     * 新增章节内容并级联更新对应某章节的章节内容集合顺序
     * @param contentDTO 需要新增的章节内容
     * @param contents 更新后的章节内容id集合（集合元素按第几章节内章节内容的顺序）
     */
    @PostMapping("/save")
    public void save(@RequestBody SectionContentDTO contentDTO,
                     @RequestBody List<String> contents) {
        sectionContentManageService.save(contentDTO, contents);
    }


    /**
     * 查看节内容详细信息
     */
    @GetMapping("/query-by-sectionId")
    public List<SectionContentVO> queryBySectionId(@RequestParam String sectionId) {
        return sectionContentManageService.queryBySectionId(sectionId);
    }

    @PostMapping("/bindSectionContent")
    public void bindSectionContext(@RequestBody List<SectionBindContextDTO> bindContextDTO){
        sectionContentManageService.bindSectionContext(bindContextDTO);
    }

    @PostMapping("/bindSectionContentAndNext")
    public void bindSectionContentAndNext(@RequestBody List<SectionBindContextDTO> bindContextDTO){
        sectionContentManageService.bindSectionContext(bindContextDTO);
        // 修改课程状态
        if(bindContextDTO.size()>0){
            String courseId = sectionDao.getById(bindContextDTO.get(0).getSectionId()).getCourseId();
            CourseDO courseDO = CourseDO.builder().id(courseId).status(2).build();
            courseDao.updateById(courseDO);
        }
    }

    /**
     * 更新sectionContent
     */
    @PostMapping("/update")
    public String update(@RequestBody SectionContentUpdateDTO updateDTO){
        return sectionContentManageService.update(updateDTO);
    }

    /**
     * 删除章节内容并级联更新对应某章节的章节内容集合顺序
     * @param sectionContentId 要删除的章节内容id
     * @param sectionId 级联更新的章节id
     * @param contentIds 更新后的章节内容id集合（集合元素按第几章节内章节内容的顺序）
     */
    @DeleteMapping("/delete/{sectionId}")
    public void delete(@PathVariable String sectionContentId,
                       @RequestParam String sectionId,
                       @RequestBody List<String> contentIds) {
        SectionDTO sectionDTO = SectionDTO.builder()
                .id(sectionId).contentIds(contentIds).build();
        sectionContentManageService.delete(sectionId, sectionDTO);
    }
}
